package com.car.common;

import org.apache.log4j.Logger;

import com.alibaba.druid.filter.stat.StatFilter;
import com.car.action.admin.BlackListController;
import com.car.action.admin.CardController;
import com.car.action.admin.CertController;
import com.car.action.admin.LoginController;
import com.car.action.admin.MessageController;
import com.car.action.admin.NavyUserController;
import com.car.action.admin.OrderController;
import com.car.action.admin.PicController;
import com.car.action.admin.PropController;
import com.car.action.admin.PulpController;
import com.car.action.admin.ResumeController;
import com.car.action.admin.TagsController;
import com.car.action.admin.UsersController;
import com.car.action.api.AlbumsController;
import com.car.action.api.AreaController;
import com.car.action.api.BannersController;
import com.car.action.api.ServiceController;
import com.car.action.api.VipController;
import com.car.action.util.UtilController;
import com.car.common.model.CardLike;
import com.car.common.model._MappingKit;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.Const;
import com.jfinal.ext.handler.ContextPathHandler;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.plugin.druid.DruidStatViewHandler;
import com.jfinal.plugin.ehcache.EhCachePlugin;
import com.jfinal.plugin.redis.RedisPlugin;
import com.jfinal.render.ViewType;
//import com.jfinal.server.undertow.UndertowServer;
import com.jfinal.template.Engine;

import cn.dreampie.quartz.QuartzPlugin;

/**
 * 
 * API引导式配置
 */
public class DefaultConfig extends JFinalConfig {
	
	private static Logger logger = Logger.getLogger(DefaultConfig.class);
	
	/**
	 * 配置常量
	 */
	public void configConstant(Constants me) {
		// 加载少量必要配置，随后可用PropKit.get(...)获取值
		PropKit.use("config.properties");
		me.setViewType(ViewType.FREE_MARKER);
		me.setDevMode(PropKit.getBoolean("devMode", false));
		me.setMaxPostSize(10000 * Const.DEFAULT_MAX_POST_SIZE);
	}
	
	
	/**
	 * 配置路由
	 */
	public void configRoute(Routes me) {
		me.setBaseViewPath("/WEB-INF/view");
		me.add("/", LoginController.class, "/login");	// 第三个参数为该Controller的视图存放路径
		
		me.add("/util", UtilController.class);
		me.add("/area", AreaController.class);
		me.add("/banners", BannersController.class);
		me.add("/albums", AlbumsController.class);
		me.add("/vip", VipController.class);
		me.add("/service", ServiceController.class, "/service");
		me.add("/card", com.car.action.api.CardController.class);
		me.add("/card_new", com.car.action.api.CardNewController.class);
		me.add("/shares", com.car.action.api.SharesController.class);
		me.add("/circle", com.car.action.api.CircleController.class);
		me.add("/message", com.car.action.api.MessageController.class);
		me.add("/sms", com.car.action.api.SmsController.class);
		me.add("/user", com.car.action.api.UserController.class);
		me.add("/user_info", com.car.action.api.UserInfoController.class);
		me.add("/find_friend", com.car.action.api.FindFriendController.class);
		me.add("/qiniu", com.car.action.api.QiniuController.class);
		me.add("/topic", com.car.action.api.TopicController.class);
		me.add("/tag", com.car.action.api.TagController.class);
		me.add("/friend", com.car.action.api.FriendController.class);
		me.add("/fans", com.car.action.api.FansController.class);
		me.add("/task", com.car.action.api.TaskController.class);
		me.add("/im", com.car.action.api.ImController.class);
		me.add("/order", com.car.action.api.OrderController.class);
		me.add("/callback", com.car.action.api.CallbackController.class);
		
		me.add("/pay", com.car.action.api.WeiXinController.class,"/wx");
		
		me.add("/cert", CertController.class, "/cert");
		me.add("/admin", LoginController.class, "/login");
		me.add("/black_list", BlackListController.class, "/blacklist");
		me.add("/card_list", CardController.class , "/card");
		me.add("/users_list", UsersController.class , "/users");
		me.add("/hot_order_list", OrderController.class , "/order");
		me.add("/pic_list", PicController.class , "/pic");
		me.add("/navy_user", NavyUserController.class);
		me.add("/pulp", PulpController.class);
		me.add("/prop", PropController.class);
		me.add("/tags", TagsController.class);
		me.add("/resume", ResumeController.class);
		me.add("/chat", MessageController.class);
		
		me.add("/vip_package", com.car.action.admin.VipPackageController.class,"/vip_package");
		//me.add("/shihou", com.car.action.admin.ShiHouController.class);
	}
	
	public void configEngine(Engine me) {
		me.addSharedFunction("/common/_layout.html");
		me.addSharedFunction("/common/_paginate.html");
	}
	
	/**
	 * 配置插件
	 */
	public void configPlugin(Plugins me) {
		//配置缓存插件
		me.add(new EhCachePlugin());
		logger.info("Service add EhCachePlugin success! ");
		/*
		RedisPlugin redis=new RedisPlugin("自定义cache名字", "地址(默认为127.0.0.1)","连接密码(有就填，没得就不要这个参数)");
		me.add(redis);
		*/
		
		// 配置 druid 数据库连接池插件
		DruidPlugin druidPlugin = createDruidPlugin();
		druidPlugin.addFilter(new StatFilter());
		druidPlugin.start();
		me.add(createDruidPlugin());
		
		logger.info("Service add DruidPlugin success! ");
				
		// 配置ActiveRecord插件
		ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
		// 所有映射在 MappingKit 中自动化搞定
		_MappingKit.mapping(arp);
		me.add(arp);
		
		logger.info("Service add DruidPlugin success! ");
		
		// 定时器
		QuartzPlugin quartzPlugin = new QuartzPlugin();
		quartzPlugin.setJobs("quartz.properties");
		me.add(quartzPlugin);
		 
		logger.info("Service add QuartzPlugin success! ");
	}
	
	public static DruidPlugin createDruidPlugin() {
		DruidPlugin dp = new DruidPlugin(PropKit.get("jdbc.club.url"), PropKit.get("jdbc.club.username"),
				PropKit.get("jdbc.club.password"));
		dp.set(10, 10, 1000);
		return dp;
	}

	
	/**
	 * 配置全局拦截器
	 */
	public void configInterceptor(Interceptors me) {
		
	}
	
	/**
	 * 配置处理器
	 */
	public void configHandler(Handlers me) {
		me.add(new ContextPathHandler("base"));
		DruidStatViewHandler dvh = new DruidStatViewHandler("/druid");
		me.add(dvh);
		// JavaMelody未处理
//		me.add(new ContextPathHandler("ctx"));      
//		me.add(new WebSocketHandler("^/websocket"));
	}
	
	
	public static void main(String[] args) {
		/**
		 * 特别注意：Eclipse 之下建议的启动方式
		 */
		//JFinal.start("src/main/webapp", 80, "/", 5);
//		UndertowServer.start(DefaultConfig.class, 8080, false);
		/*
		 * .configWeb( builder -> {
// 配置 Filter
builder.addFilter("myFilter", "com.abc.MyFilter");
builder.addFilterUrlMapping("myFilter", "/*");
builder.addFilterInitParam("myFilter", "key", "value");

// 配置 Servlet
builder.addServlet("myServlet", "com.abc.MyServlet");
builder.addServletMapping("myServlet", "*.do");
builder.addServletInitParam("myServlet", "key", "value");

// 配置 Listener
builder.addListener("com.abc.MyListener");

// 配置 WebSocket，MyWebSocket 需使用 ServerEndpoint 注解
builder.addWebSocketEndpoint("com.abc.MyWebSocket");
})
		 */
	}
	
}
